Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

var EXPORTED_SYMBOLS = ["HTTPUtils"];

HTTPUtils = {

	HttpErrorData: function(code, description)
	{
		this.code = code;
		this.description = description;
		this.QueryInterface = XPCOMUtils.generateQI([Components.interfaces.nsIObserver, Components.interfaces.awIHttpErrorData]);
	},
	HttpError: function(code,description) {
		return Components.Exception('HTTP Error',Components.results.NS_ERROR_FAILURE,Components.stack.caller.caller, new HTTPUtils.HttpErrorData(code, description));
	},
	getErrorCode: function (error)
	{
		if (error.data)
		{
			try
			{
				var d = error.data.QueryInterface(Components.interfaces.awIHttpErrorData);
				return d.code;
			}
			catch(e)
			{
				return -1;
			}
		}
	},
	decodeURIParameters: function(paramString)
	{
	  
		var params = {};
		
		if (paramString == '') return params;
		
		var p_list = paramString.split('&');
		var pair;
		for (var i=0;i<p_list.length;i++)
		{
			pair = p_list[i].split('=');
			
			if (pair.length < 2)
				continue;
				
			params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
		}
		return params;
	},
/**
 * Errors thrown to trigger specific HTTP server responses.
 */
	get HTTP_400() { return HTTPUtils.HttpError(400, "Bad Request"); },
	get HTTP_401() { return HTTPUtils.HttpError(401, "Unauthorized"); },
	get HTTP_402() { return HTTPUtils.HttpError(402, "Payment Required"); },
	get HTTP_403() { return HTTPUtils.HttpError(403, "Forbidden"); },
	get HTTP_404() { return HTTPUtils.HttpError(404, "Not Found"); },
	get HTTP_405() { return HTTPUtils.HttpError(405, "Method Not Allowed"); },
	get HTTP_406() { return HTTPUtils.HttpError(406, "Not Acceptable"); },
	get HTTP_407() { return HTTPUtils.HttpError(407, "Proxy Authentication Required"); },
	get HTTP_408() { return HTTPUtils.HttpError(408, "Request Timeout"); },
	get HTTP_409() { return HTTPUtils.HttpError(409, "Conflict"); },
	get HTTP_410() { return HTTPUtils.HttpError(410, "Gone"); },
	get HTTP_411() { return HTTPUtils.HttpError(411, "Length Required"); },
	get HTTP_412() { return HTTPUtils.HttpError(412, "Precondition Failed"); },
	get HTTP_413() { return HTTPUtils.HttpError(413, "Request Entity Too Large"); },
	get HTTP_414() { return HTTPUtils.HttpError(414, "Request-URI Too Long"); },
	get HTTP_415() { return HTTPUtils.HttpError(415, "Unsupported Media Type"); },
	get HTTP_416() { return HTTPUtils.HttpError(416, "Requested Range Not Satisfiable"); },
	get HTTP_417() { return HTTPUtils.HttpError(417, "Expectation Failed"); },

	get HTTP_500() { return HTTPUtils.HttpError(500, "Internal Server Error"); },
	get HTTP_501() { return HTTPUtils.HttpError(501, "Not Implemented"); },
	get HTTP_502() { return HTTPUtils.HttpError(502, "Bad Gateway"); },
	get HTTP_503() { return HTTPUtils.HttpError(503, "Service Unavailable"); },
	get HTTP_504() { return HTTPUtils.HttpError(504, "Gateway Timeout"); },
	get HTTP_505() { return HTTPUtils.HttpError(505, "HTTP Version Not Supported"); },
};